using UnityEngine;
using UnityEngine.UI;

public class UIB_SkillMask_Script : MonoBehaviour
{
    [Header("技能cd时长")]
    [Range(1, 100)]
    public float cd = 2f;
    
    private Image maskImage;
    private Text maskText;
    private bool doingCD = false;
    private float cdTimer;
    
    void Awake()
    {
        maskImage = GetComponent<Image>();
        maskImage.fillAmount = 0f;
        // maskText = transform.GetChild(0).GetComponent<Text>();
        maskText = GetComponentInChildren<Text>();
        maskText.text = "";
    }

    void Update()
    {
        // 点击鼠标左键，且技能不在cd中，释放技能
        if (Input.GetMouseButtonDown(0) && !doingCD)
        {
            // 释放完技能，fillAmount置为1
            maskImage.fillAmount = 1;
            doingCD = true;
            cdTimer = cd;
        }
        // 减cd
        if (doingCD)
        {
            // 旋转遮罩
            maskImage.fillAmount -= Time.deltaTime / cd;
            // 更新剩余时间
            cdTimer -= Time.deltaTime;
            maskText.text = cdTimer.ToString("0.00");
            // 如果cd完成，则设置doingCD = false;
            if (maskImage.fillAmount == 0)
            {
                doingCD = false;
                maskText.text = "";
            }
        }
    }
}
